﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;
using System.Windows.Data;

namespace ConstellationShow
{
    public class Com
    {
        public static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        private static string[] _constellationName = 
                    { 
                    "白羊座", "金牛座", "双子座", 
                    "巨蟹座", "狮子座", "处女座", 
                    "天秤座", "天蝎座", "射手座", 
                    "摩羯座", "水瓶座", "双鱼座"
                    };

        //查询子项 用法：FindChildOfType<ScrollViewer>(listbox)
        public static T FindChildOfType<T>(DependencyObject root) where T : class
        {
            var queue = new Queue<DependencyObject>();
            queue.Enqueue(root);
            while (queue.Count > 0)
            {
                DependencyObject current = queue.Dequeue();
                for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
                {
                    var child = VisualTreeHelper.GetChild(current, i);
                    var typedChild = child as T;
                    if (typedChild != null)
                    {
                        return typedChild;
                    }
                    queue.Enqueue(child);
                }
            }
            return null;
        }

        //解析运程
        public static List<string> ParseLuck(string s, int index)
        {
            List<string> lst = new List<string>();
            string[] ss = Regex.Split(s, "<br>", RegexOptions.IgnoreCase);
            foreach (string str in ss)
            {
                if (str == "" || string.IsNullOrEmpty(str)) continue;
                if (index == 0 || index == 1)
                {
                    if (str.IndexOf("今日运势短评：") > -1)
                        lst.Add(str.Replace("今日运势短评：", ""));
                    if (str.IndexOf("贵人星座：") > -1)
                        lst.Add(str.Replace("贵人星座：", "").Replace("·", ""));
                    if (str.IndexOf("幸运数字：") > -1)
                        lst.Add(str.Replace("幸运数字：", "").Replace("·", ""));
                    if (str.IndexOf("吉时吉色：") > -1)
                    {
                        string temp = str.Replace("吉时吉色：", "").Replace("·", "");
                        string[] tmp = temp.Split(' ');
                        foreach (string t in tmp)
                            if (t != "" && !string.IsNullOrEmpty(t)) lst.Add(t);
                    }
                    else if (str.IndexOf("开运方位：") > -1)
                        lst.Add(str.Replace("开运方位：", "").Replace("·", ""));
                }


                if (index == 2)
                {
                    if (str.IndexOf("本周致胜金言：") > -1)
                        lst.Add(str.Replace("本周致胜金言：", ""));

                    if (str.IndexOf("幸运色彩：") > -1)
                    {
                        string[] s3 = str.Split(' ');
                        foreach (string s4 in s3)
                        {
                            if (s4 == "" || string.IsNullOrEmpty(s4)) continue;
                            lst.Add(s4.Replace("幸运色彩：", "").Replace("幸运日：", ""));
                        }
                    }

                    if (str.IndexOf("贵人星座：") > -1)
                    {
                        string[] s3 = Regex.Split(str, " 幸", RegexOptions.IgnoreCase);
                        foreach (string s4 in s3)
                            lst.Add(s4.Replace("贵人星座：", "").Replace("运数字：", "").Trim());
                    }

                    if (str.IndexOf("小人星座：") > -1)
                    {
                        string[] s3 = Regex.Split(str, " 吉", RegexOptions.IgnoreCase);
                        foreach (string s4 in s3)
                            lst.Add(s4.Replace("小人星座：", "").Replace("运方位：", "").Trim());
                    }

                }

                if (index == 3)
                {
                    if (str.IndexOf("本月优势：") > -1)
                        lst.Add(str.Replace("本月优势：", ""));
                    if (str.IndexOf("本月弱势：") > -1)
                        lst.Add(str.Replace("本月弱势：", ""));
                    if (str.IndexOf("贵人方位：") > -1)
                        lst.Add(str.Replace("贵人方位：", ""));
                    if (str.IndexOf("开运物品：") > -1)
                        lst.Add(str.Replace("开运物品：", ""));
                    if (str.IndexOf("休闲解压：") > -1)
                        lst.Add(str.Replace("休闲解压：", ""));
                }




            }
            return lst;
        }

        //解析运程2
        public static List<string> ParseLuck2(string s)
        {
            List<string> lst = new List<string>();
            s = s.Replace("<br><span class=\"chuti\">", "\n").Replace("</span><br>", "")
                .Replace("<span class=\"chuti\">", "\n").Replace("</span>", "");
            string[] ss = Regex.Split(s, "<br>", RegexOptions.IgnoreCase);
            foreach (string str in ss)
            {
                if (str != "" && !string.IsNullOrEmpty(str))
                {
                    if (str.IndexOf("整体运:") > -1)
                        lst.Add(str.Replace("整体运:", ""));
                    else if (str.IndexOf("事业运:") > -1)
                        lst.Add(str.Replace("事业运:", ""));
                    else if (str.IndexOf("爱情运:") > -1)
                        lst.Add(str.Replace("爱情运:", ""));
                    else if (str.IndexOf("财运:") > -1)
                        lst.Add(str.Replace("财运:", ""));
                }
            }
            return lst;
        }

        public static string SPstr(string str, int length)
        {
            if (str.Length >= length)
                return str.Substring(0, length - 1) + "..";
            else
                return str;
        }

        public static string ConsMonth(int c)
        {
            if (c == 0) return "3.21-4.20";
            else if (c == 1) return "4.21-5.21";
            else if (c == 2) return "5.22-6.21";
            else if (c == 3) return "6.22-7.22";
            else if (c == 4) return "7.23-8.23";
            else if (c == 5) return "8.24-9.23";
            else if (c == 6) return "9.24-10.23";
            else if (c == 7) return "10.24-11.22";
            else if (c == 8) return "11.23-12.21";
            else if (c == 9) return "12.22-1.20";
            else if (c == 10) return "1.21-2.19";
            else if (c == 11) return "2.20-3.20";
            return "";
        }

        public static string ConsImg(int c)
        {
            if (c == 0) return "icon_03.png";
            else if (c == 1) return "icon_04.png";
            else if (c == 2) return "icon_05.png";
            else if (c == 3) return "icon_06.png";
            else if (c == 4) return "icon_07.png";
            else if (c == 5) return "icon_08.png";
            else if (c == 6) return "icon_09.png";
            else if (c == 7) return "icon_10.png";
            else if (c == 8) return "icon_11.png";
            else if (c == 9) return "icon_12.png";
            else if (c == 10) return "icon_01.png";
            else if (c == 11) return "icon_02.png";
            return "";
        }

        //设置
        public static object GetSetting(string key)
        {
            if (settings.Contains(key))
                return settings[key];
            else
                return null;
        }

        //获取设置
        public static void SetSetting(string key, object value)
        {
            if (settings.Contains(key))
                settings[key] = value;
            else
                settings.Add(key, value);
            settings.Save();
        }

        public static void Share(string title, string str)
        {
            var shareLinkTask = new ShareLinkTask();
            shareLinkTask.Title = title;
            shareLinkTask.LinkUri = new Uri("http://f.lexun.com/touch/xingzuo.aspx?vs=2", UriKind.Absolute);
            shareLinkTask.Message = str;
            shareLinkTask.Show();
        }

        //十六进制颜色转Color对象
        public static Color HtmlToColor(string htmlColr)
        {
            int baseIndex = 1;
            byte a, r, g, b;
            a = r = g = b = 255;
            if (htmlColr.Length == 9)
            {
                a = Convert.ToByte(htmlColr.Substring(baseIndex, 2), 16);
                baseIndex += 2;
            }
            r = Convert.ToByte(htmlColr.Substring(baseIndex, 2), 16);
            g = Convert.ToByte(htmlColr.Substring(baseIndex += 2, 2), 16);
            b = Convert.ToByte(htmlColr.Substring(baseIndex += 2, 2), 16);
            return Color.FromArgb(a, r, g, b);
        }

        public static string ConvCons(DateTime _date)
        {
            
            int index = 0;
            int y, m, d;
            y = _date.Year;
            m = _date.Month;
            d = _date.Day;
            y = m * 100 + d;

            if (((y >= 321) && (y <= 419))) { index = 0; }
            else if ((y >= 420) && (y <= 520)) { index = 1; }
            else if ((y >= 521) && (y <= 620)) { index = 2; }
            else if ((y >= 621) && (y <= 722)) { index = 3; }
            else if ((y >= 723) && (y <= 822)) { index = 4; }
            else if ((y >= 823) && (y <= 922)) { index = 5; }
            else if ((y >= 923) && (y <= 1022)) { index = 6; }
            else if ((y >= 1023) && (y <= 1121)) { index = 7; }
            else if ((y >= 1122) && (y <= 1221)) { index = 8; }
            else if ((y >= 1222) || (y <= 119)) { index = 9; }
            else if ((y >= 120) && (y <= 218)) { index = 10; }
            else if ((y >= 219) && (y <= 320)) { index = 11; }
            else { index = 0; }

            return _constellationName[index];
        }
        public static string Nongli(DateTime d) //d为农历
        {
            ConstellationShow.ConvChineseData.ChineseCalendar cc =
                new ConstellationShow.ConvChineseData.ChineseCalendar(d.Year, d.Month, d.Day, false);
            return cc.AnimalString + " " + cc.GanZhiYearString + " " + cc.ChineseDateString;
        }
        public static string Nongli1(DateTime d) //d为阳历
        {
            ConstellationShow.ConvChineseData.ChineseCalendar cc =
                new ConstellationShow.ConvChineseData.ChineseCalendar(d);
            return cc.AnimalString + " " + cc.GanZhiYearString + " " + cc.ChineseDateString;
        }
    }

    public class Cons
    {
        public string name { get; set; }
        public string id { get; set; }
    }

    public class DataStr : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value.ToString() == "") return "";
            return DateTime.Parse(value.ToString()).ToString("yyyy年M月dd日");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class DataStr2 : IValueConverter
    {
        public static DateTime date2;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value.ToString() == "") return "";
            DateTime d = DateTime.Parse(value.ToString());
            date2 = d;
            return Com.Nongli(d);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
